<template>
  <el-dialog
    v-model="dialogVisible"
    :title="mode === 'create' ? '新增培训记录' : '编辑培训记录'"
    width="700px"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="员工" prop="employee.id">
        <el-select 
          v-model="formData.employee.id" 
          placeholder="请选择员工"
          filterable
        >
          <el-option
            v-for="employee in employees"
            :key="employee.id"
            :label="employee.name"
            :value="employee.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="formData.courseName" placeholder="请输入课程名称" />
      </el-form-item>

      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="formData.courseType" placeholder="请选择课程类型">
          <el-option label="技术培训" value="技术培训" />
          <el-option label="管理培训" value="管理培训" />
          <el-option label="业务培训" value="业务培训" />
          <el-option label="软技能" value="软技能" />
          <el-option label="认证培训" value="认证培训" />
        </el-select>
      </el-form-item>

      <el-form-item label="培训日期" prop="trainingDate">
        <el-date-picker
          v-model="formData.trainingDate"
          type="date"
          placeholder="选择培训日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="考核分数" prop="score">
            <el-input-number
              v-model="formData.score"
              :min="0"
              :max="10"
              :step="0.1"
              controls-position="right"
              placeholder="请输入考核分数"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="培训时长" prop="trainingHours">
            <el-input-number
              v-model="formData.trainingHours"
              :min="1"
              :max="200"
              controls-position="right"
              placeholder="培训时长"
            />
            <span style="margin-left: 8px">小时</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="讲师" prop="instructor">
        <el-input v-model="formData.instructor" placeholder="请输入讲师姓名" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="进行中">进行中</el-radio>
          <el-radio label="已完成">已完成</el-radio>
          <el-radio label="已取消">已取消</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="获得证书" prop="certificate">
        <el-input v-model="formData.certificate" placeholder="请输入证书名称（如无证书请留空）" />
      </el-form-item>

      <el-form-item label="备注">
        <el-input
          v-model="formData.remarks"
          type="textarea"
          :rows="3"
          placeholder="请输入培训备注信息"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'TrainingDialog',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    training: {
      type: Object,
      default: null
    },
    employees: {
      type: Array,
      default: () => []
    },
    mode: {
      type: String,
      default: 'create'
    }
  },
  emits: ['update:modelValue', 'save', 'close'],
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    const formRef = ref()
    const loading = ref(false)

    const formData = ref({
      employee: {
        id: ''
      },
      courseName: '',
      courseType: '',
      trainingDate: '',
      score: 0,
      trainingHours: 8,
      instructor: '',
      status: '已完成',
      certificate: '',
      remarks: ''
    })

    const rules = {
      'employee.id': [
        { required: true, message: '请选择员工', trigger: 'change' }
      ],
      courseName: [
        { required: true, message: '请输入课程名称', trigger: 'blur' }
      ],
      courseType: [
        { required: true, message: '请选择课程类型', trigger: 'change' }
      ],
      trainingDate: [
        { required: true, message: '请选择培训日期', trigger: 'change' }
      ],
      score: [
        { required: true, message: '请输入考核分数', trigger: 'blur' }
      ],
      instructor: [
        { required: true, message: '请输入讲师姓名', trigger: 'blur' }
      ],
      status: [
        { required: true, message: '请选择状态', trigger: 'change' }
      ]
    }

    watch(
      () => props.modelValue,
      (newVal) => {
        dialogVisible.value = newVal
        if (newVal && props.training) {
          // 编辑模式下填充数据
          nextTick(() => {
            formData.value = { 
              ...props.training,
              employee: {
                id: props.training.employeeId || ''
              }
            }
          })
        } else if (newVal) {
          // 创建模式下重置数据
          formData.value = {
            employee: {
              id: ''
            },
            courseName: '',
            courseType: '',
            trainingDate: '',
            score: 0,
            trainingHours: 8,
            instructor: '',
            status: '已完成',
            certificate: '',
            remarks: ''
          }
        }
      }
    )

    watch(dialogVisible, (newVal) => {
      emit('update:modelValue', newVal)
    })

    const handleClose = () => {
      dialogVisible.value = false
      emit('close')
    }

    const handleSubmit = async () => {
      if (!formRef.value) return

      try {
        const valid = await formRef.value.validate()
        if (valid) {
          loading.value = true
          emit('save', { ...formData.value })
        }
      } catch (error) {
        ElMessage.error('请完善表单信息')
      } finally {
        loading.value = false
      }
    }

    return {
      dialogVisible,
      formRef,
      formData,
      rules,
      loading,
      handleClose,
      handleSubmit
    }
  }
}
</script>